package cn.xfdzx.android.apps.shop.activity.login;

import android.content.Context;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.Toast;

import com.geetest.onelogin.OneLoginHelper;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.umeng.socialize.PlatformConfig;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.MainActivity;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.apps.MyApp;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.LogininfoBean;
import cn.xfdzx.android.apps.shop.contract.OtherLoginContract;
import cn.xfdzx.android.apps.shop.evenbus.CarInitDataMessage;
import cn.xfdzx.android.apps.shop.evenbus.IMloginMessage;
import cn.xfdzx.android.apps.shop.evenbus.WeChatCodeEvent;
import cn.xfdzx.android.apps.shop.evenbus.WeChatLoginEvent;
import cn.xfdzx.android.apps.shop.fragment.OneFragment;
import cn.xfdzx.android.apps.shop.presenter.OtherLoginPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.GlideUtils;
import cn.xfdzx.android.apps.shop.util.Holder;
import cn.xfdzx.android.apps.shop.util.OneLoginResult;
import cn.xfdzx.android.apps.shop.util.OneLoginUtils;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.util.WindowUtils;
import cn.xfdzx.android.apps.shop.wxapi.WXEntryActivity;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;
import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

public class OneLoginMainActivity extends BaseMvpActivity<OtherLoginPresenter> implements OtherLoginContract.View {

    private ImageView phoneImage;
    private boolean isPortrait = false;
    private OneLoginUtils oneLoginUtils;


    @Override
    public int getLayoutId() {
        return R.layout.activity_one_login_main;
    }

    @Override
    public void initView() {

        mPresenter = new OtherLoginPresenter();
        mPresenter.attachView(this);
        EventBus.getDefault().register(this);
        phoneImage = findViewById(R.id.phone_iv);
        getWindow().setWindowAnimations(R.style.AnimBottom);
        oneLoginUtils = new OneLoginUtils(OneLoginMainActivity.this, oneLoginResult);
        oneLoginUtils.setSenseBotEnable(Holder.with().isEnableSenseBot());
        if (Holder.with().getOneLoginStyle() == Constants.ONELOGIN_FULLSCREEN_MODE) {
            isPortrait = true;
        } else {
            isPortrait = false;
        }
        OneLoginHelper.with().setRequestedOrientation(OneLoginMainActivity.this, isPortrait);

        WindowUtils.setStatusBar(this);
        GlideUtils.loadGifResource(this, R.drawable.getphone, phoneImage);
        oneLoginUtils.requestToken();

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onLoginCodeSuccess(LogininfoBean bean) {

    }

    @Override
    public void onSendSmsSuccess(LogininfoBean bean) {

    }

    @Override
    public void onCheckBindPhoneSuccess(LogininfoBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (bean.getData().isIs_login()) {
                APP_TOKEN = bean.getData().getToken();
                UtilsData.getPreference(this, "login").put("token", APP_TOKEN);
                UtilsData.getPreference(this, "login").put("is_store", EmptyUtil.isEmpty(bean.getData().getUserInfo().getIs_store()) ? 0 : bean.getData().getUserInfo().getIs_store());
                UtilsData.getPreference(this, "login").put("nickname", EmptyUtil.isEmpty(bean.getData().getUserInfo().getNickname()) ? "" : bean.getData().getUserInfo().getNickname());
                UtilsData.getPreference(this, "login").put("head_url", EmptyUtil.isEmpty(bean.getData().getUserInfo().getAvatar()) ? "" : bean.getData().getUserInfo().getAvatar());
//                Intent intent = new Intent(this, MainActivity.class);
//                intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
//                intent.addFlags(FLAG_ACTIVITY_SINGLE_TOP);
//                startActivity(intent);
                EventBus.getDefault().post(new IMloginMessage(1));
                oneLoginUtils.onVerifyEnd();
                finish();
            } else {
                Intent intent = new Intent(this, MobileBindActivity.class);
                if (EmptyUtil.isNotEmpty(bean.getData().getHash())) {
                    intent.putExtra("loginhash", bean.getData().getHash());
                }
                startActivity(intent);
                oneLoginUtils.onVerifyEnd();
                finish();
            }
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void wechatLoginSuccess(LogininfoBean bean) {

    }

    private OneLoginResult oneLoginResult = new OneLoginResult() {
        /**
         * 授权页面返回结果后回调处理，关闭当前页面
         */
        @Override
        public void onResult() {
            finish();
        }

        @Override
        public void onResult(int flag) {
            super.onResult(flag);
            onResult();
        }
    };


    /**
     * 微信登录点击回调
     **/
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXXX(WeChatLoginEvent messageEvent) {
        //Utils.toastMessage(this, "11111111111");
        //解决微信sdk登录和友盟三方sdk冲突问题，分享页面重新set
        PlatformConfig.setWeixin("", "");
        if (MyApp.isWeixinAvilible(OneLoginMainActivity.this)) {
            WXEntryActivity.loginWeixin(OneLoginMainActivity.this, MyApp.mWxApi);
        } else {
            Toast.makeText(this, "请先安装微信", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 微信授权code回调
     **/
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void loginWechat(WeChatCodeEvent weChatCodeEvent) {
        if (EmptyUtil.isEmpty(weChatCodeEvent.getCode())) {
            Utils.toastMessage(this, "授权失败");
            return;
        }
        //MyApp.mWxApi.unregisterApp();
        //请求检测APP微信登录是否绑定手机号
        mPresenter.checkBindPhone(weChatCodeEvent.getCode());

//        Intent intent = new Intent(this, MobileBindActivity.class);
//
//        startActivity(intent);

    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        mPresenter.detachView();
        super.onDestroy();
    }

}
